local currentDir = function()
    local info = debug.getinfo(2)
    local path = info.source
    path = string.sub(path, 2, -1)
    path = string.gsub(path,'\\','/')
    path = string.match(path, "^(.*)/")
    return path
end



-- 优化：只在初始化时读取敏感词
local sensitive_words = {}

local function load_sensitive_words()
    local file1 = io.open(currentDir().."/sensitive_words.lua", "r")
    if not file1 then
        return {}
    end
    local lines = {}
    for line in file1:lines() do
        if line and #line > 0 then
            table.insert(lines, line)
        end
    end
    file1:close()
    return lines
end

local function contains(table, value)
    for _, v in ipairs(table) do
        if v == value then
            return true
        end
    end
    return false
end

local codeShield = {}

function codeShield.init(env)
    sensitive_words = load_sensitive_words()
end

function codeShield.func(input, env)
    for cand in input:iter() do
        if string.len(cand.text) < 1 then
            return 1
        end
        if contains(sensitive_words, cand.text) then 
            yield(Candidate("word", cand.start, cand._end,  "**", cand.comment))
        else
            yield(cand)
        end
    end
end

return codeShield